package com.thssh.ui.lsn08.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

/**
 * @author zhangyugehu
 * @version V1.0
 * @date 2017/09/21
 * email: zhangyugehu@163.com
 */

public class SearchView extends View {

    // 圆半径
    private static final float RADIUS = 20;
    // 把手长度
    private static final float SMALL_LINE_LENGTH = 30;
    // 圆起始角度偏移
    private static final float ANGLE = 45;
    private static final float LINE_WIDTH = 5;

    private boolean showSmallLine;
    private boolean showCircle;

    private int mWidth = 600;
    private int mHeight = 400;

    private Paint mPaint;
    private RectF rectF;

    private ValueAnimator mOpenAnimator;
    private ValueAnimator mCloseAnimator;
    private float mPercent;
    // 把手离圆端起始x
    private float mSmallLineX = RADIUS;
    // 圆起始角度
    private float startAngle;
    // 把手旋转后的终点X
    private float smallLineRotateEndX;
    // 以把手所作的等腰直角三角形边长，即圆心到横线的距离
    private float smallLineHeight;


    public SearchView(Context context) {
        this(context, null);
    }

    public SearchView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SearchView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        rectF = new RectF();
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.GRAY);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(LINE_WIDTH);
        mOpenAnimator = ValueAnimator.ofFloat(0, 1);
        mOpenAnimator.setDuration(500);
        mOpenAnimator.setInterpolator(new LinearInterpolator());
        mOpenAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mPercent = (float) animation.getAnimatedValue();
                invalidate();
            }
        });
        mCloseAnimator = ValueAnimator.ofFloat(1, 0);
        mCloseAnimator.setDuration(500);
        mCloseAnimator.setInterpolator(new LinearInterpolator());
        mCloseAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mPercent = (float) animation.getAnimatedValue();
                invalidate();
            }
        });
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        smallLineRotateEndX = RADIUS + SMALL_LINE_LENGTH;
        smallLineHeight = (float) Math.sqrt(smallLineRotateEndX * smallLineRotateEndX / 2.0f);
        mHeight = (int) (smallLineHeight + RADIUS + LINE_WIDTH * 4);
        if(MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY){
            mWidth = MeasureSpec.getSize(widthMeasureSpec);
        }
        if(MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.EXACTLY){
            mHeight = MeasureSpec.getSize(heightMeasureSpec);
        }
        setMeasuredDimension(mWidth, mHeight);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    private void resetClose(){
        startAngle = ANGLE;
        mSmallLineX = RADIUS;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 圆右移的偏移量
        float circleDx = mPercent * (mWidth / 2 - smallLineHeight);
        // 横线的偏移量
        float lineDx = mPercent * mWidth / 2;

        // 圆心
        float centerX = mWidth / 2 + circleDx;
        float centerY = mHeight / 2;
        // 圆的外切矩形
        float left = centerX - RADIUS;
        float top = centerY - RADIUS;
        float right = centerX  + RADIUS;
        float bottom = centerY + RADIUS;
        rectF.set(left, top, right, bottom);

        // 把手终点
        float smallLineEndX = (float) (smallLineHeight + centerX);
        float smallLineEndY = (float) (smallLineHeight + centerY);

        // 圆和把手共用整个进度，所以重新计算圆和把手进度
        float circlePercent = mPercent * 2;
        startAngle = 360 * circlePercent + ANGLE;
        float sweepAngle = 360 - startAngle + ANGLE;

        float lineStartX = smallLineEndX - lineDx * 2;
        float lineStartY = smallLineEndY;
        float lineEndX = smallLineEndX;
        float lineEndY = smallLineEndY;

        if(startAngle > 360 + ANGLE){
            // 圆消失
            float smallLinePercent = (mPercent - 0.5f) * 2.0f;
            mSmallLineX = SMALL_LINE_LENGTH * smallLinePercent + RADIUS;
            showCircle = false;
        }else{
            showCircle = true;
        }
        if(mSmallLineX > smallLineRotateEndX){
            // 把手消失
            showSmallLine = false;
        }else{
            showSmallLine = true;
        }


        if(showCircle) {
            // 画圆
            canvas.drawArc(rectF, startAngle, sweepAngle, false, mPaint);
        }


        if(showSmallLine) {
            // 画把手
            canvas.save();
            canvas.translate(centerX, centerY);
            canvas.rotate(45);
            canvas.drawLine(mSmallLineX, 0, smallLineRotateEndX, 0, mPaint);
            canvas.restore();
        }

        // 画横线
        canvas.drawLine(lineStartX, lineStartY, lineEndX, lineEndY, mPaint);

//        drawCenterLines(centerX, centerY, canvas);
    }

    private void drawCenterLines(float centerX, float centerY, Canvas canvas) {
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        canvas.drawLine(0, centerY, mWidth, centerY, paint);
        canvas.drawLine(centerX, 0, centerX, mHeight, paint);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if(mOpenAnimator != null){
            mOpenAnimator.cancel();
        }
        if(mCloseAnimator != null){
            mCloseAnimator.cancel();
        }
        if(mPaint != null){
            mPaint = null;
        }
        if(rectF != null){
            rectF = null;
        }
    }

    public void open(){
        if(mOpenAnimator == null){ return; }
        resetClose();
        mOpenAnimator.start();
    }

    public void close(){
        if(mCloseAnimator == null){ return; }
        mCloseAnimator.start();
    }
}
